package com.sos.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sos.widget.R;

/**
 * 自定义组件：购买数量，带减少增加按钮
 * Created by hiwhitley on 2016/7/4.
 */
public class AmountView extends LinearLayout implements View.OnClickListener, TextWatcher, Runnable {

    private static final String TAG = "AmountView";
    private int amount = 1; //购买数量
    private int goods_storage = Integer.MAX_VALUE; //商品库存

    private OnAmountChangeListener mListener;

    private EditText etAmount;
    private ImageView btnDecrease;
    private ImageView btnIncrease;

    public AmountView(Context context) {
        this(context, null);
    }

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.view_amount, this);
        etAmount = (EditText) findViewById(R.id.etAmount);
        btnDecrease = (ImageView) findViewById(R.id.btnDecrease);
        btnIncrease = (ImageView) findViewById(R.id.btnIncrease);
        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        etAmount.addTextChangedListener(this);

        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView);
        int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnWidth, LayoutParams.WRAP_CONTENT);
        int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvWidth, 80);
        int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvTextSize, 0);
        int btnTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnTextSize, 13);
        int color = obtainStyledAttributes.getColor(R.styleable.AmountView_btnCenterColor, Color.TRANSPARENT);
        int textColor = obtainStyledAttributes.getColor(R.styleable.AmountView_btnTextColor, Color.parseColor("#333333"));
        int leftRes = obtainStyledAttributes.getResourceId(R.styleable.AmountView_btnLeftDrawable, R.drawable.ic_lose);
        int rightRes = obtainStyledAttributes.getResourceId(R.styleable.AmountView_btnRightDrawable, R.drawable.ic_add);

        obtainStyledAttributes.recycle();

        LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT);
/*        btnDecrease.setLayoutParams(btnParams);
        btnIncrease.setLayoutParams(btnParams);*/

        LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT);
        etAmount.setLayoutParams(textParams);
        if (tvTextSize != 0) {
            etAmount.setTextSize(tvTextSize);
        }
        etAmount.setTextColor(textColor);
        etAmount.setBackgroundColor(color);
        btnDecrease.setImageResource(leftRes);
        btnIncrease.setImageResource(rightRes);
        etAmount.setTextSize(btnTextSize);

    }

    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }

    public void enableClick(boolean hide) {
        if (hide) {
            btnDecrease.setVisibility(View.INVISIBLE);
            btnIncrease.setVisibility(View.INVISIBLE);
            etAmount.setFocusable(false);
            etAmount.setClickable(false);
            etAmount.setFilterTouchesWhenObscured(false);
        } else {
            btnDecrease.setVisibility(View.VISIBLE);
            btnIncrease.setVisibility(View.VISIBLE);
            etAmount.setFocusable(true);
            etAmount.setClickable(true);
            etAmount.setFilterTouchesWhenObscured(true);
        }
    }


    public void setGoods_storage(int goods_storage) {
        this.goods_storage = goods_storage;
        if (amount > goods_storage) {
            etAmount.setText(goods_storage + "");
            return;
        }
    }


    public void setAmountAndStorage(int amount, int goods_storage) {
        this.goods_storage = goods_storage;
        if (amount <= goods_storage) {
            etAmount.setText(amount + "");
            return;
        }else{
            this.goods_storage=1;
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btnDecrease) {
            if (amount > 1) {
                amount--;
                etAmount.setText(amount + "");
//                if (mListener != null) {
//                    mListener.onAmountChange(this, amount, amount < goods_storage);
//                }
            }
        } else if (i == R.id.btnIncrease) {
            if (amount < goods_storage) {
                amount++;
                etAmount.setText(amount + "");
//                if (mListener != null) {
//                    mListener.onAmountChange(this, amount, amount < goods_storage);
//                }
            }
        }
        etAmount.clearFocus();


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }


    Handler handler = new Handler();

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty()){
            if (goods_storage >=1) {
                etAmount.setText("1");
            }else{
                etAmount.setText("0");
            }
            return;
        }

        amount = Integer.valueOf(s.toString());
        if (amount > goods_storage) {
            etAmount.setText(goods_storage + "");
            return;
        }
        if (amount < 0) {
            etAmount.setText("1");
        }
        if(mListener != null){
            handler.removeCallbacks(this);
            handler.postDelayed(this, 300);
        }

    }


    public int getAmount() {
        return amount;
    }

    @Override
    public void run() {
        if (mListener != null) {
            mListener.onAmountChange(this, amount, amount <= goods_storage);
        }
    }

    public interface OnAmountChangeListener {
        void onAmountChange(View view, int amount, boolean socket);
    }

}